package com.liming.admin.controller;

import com.liming.common.pojo.dto.product.AddProductDTO;
import com.liming.common.pojo.dto.product.ProductPageQuery;
import com.liming.common.pojo.dto.product.UpdateProductDTO;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.product.openfeign.FeignProductVO;
import com.liming.common.pojo.vo.product.web.ProductVO;
import com.liming.common.result.LMResult;
import com.liming.common.util.PageUtils;
import com.liming.product.client.ProductClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author liming
 * @date 2025/1/16
 */
@Slf4j
@RestController
@RequestMapping("/admin/product")
@AllArgsConstructor
@Tag(name = "商品接口-Web")
public class ProductController {

    private final ProductClient productClient;

    @Operation(
            summary = "分页",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = ProductVO.class))
                    }),
                    @ApiResponse(responseCode = "-1", description = "获取失败", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = LMResult.class))
                    })
            }
    )
    @GetMapping
    public LMResult<PageVO<ProductVO>> page(ProductPageQuery query) {
        PageVO<FeignProductVO> feignResult = productClient.page(query);
        PageVO<ProductVO> result = new PageVO<>();
        PageUtils.copyProperties(feignResult, result, ProductVO.class);
        return LMResult.success(result);
    }

    @Operation(
            summary = "详情",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = ProductVO.class))
                    }),
                    @ApiResponse(responseCode = "-1", description = "获取失败", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = LMResult.class))
                    })
            }
    )
    @GetMapping("{id}")
    public LMResult<ProductVO> get(@PathVariable Long id){
        FeignProductVO feignResult = productClient.get(id);
        ProductVO result = new ProductVO();
        BeanUtils.copyProperties(feignResult, result);
        return LMResult.success(result);
    }

    @Operation(summary = "新增")
    @PostMapping
    public LMResult<Boolean> add(@RequestBody AddProductDTO addProductDTO){
        return LMResult.success(productClient.add(addProductDTO));
    }

    @Operation(summary = "Excel批量新增")
    @PostMapping("excel")
    public LMResult<Boolean> importProduct(@RequestParam MultipartFile file) {
        return LMResult.success(productClient.importProduct(file));
    }

    @Operation(summary = "更新")
    @PutMapping
    public LMResult<Boolean> update(@RequestBody UpdateProductDTO updateProductDTO){
        return LMResult.success(productClient.update(updateProductDTO));
    }

    @Operation(summary = "商品上架/下架")
    @PutMapping("{id}")
    public LMResult<Boolean> updateOnShelf(@PathVariable Long id) {
        return LMResult.success(productClient.updateOnShelf(id));
    }

    @Operation(summary = "删除")
    @DeleteMapping("{id}")
    public LMResult<Boolean> delete(@PathVariable Long id){
        return LMResult.success(productClient.delete(id));
    }

    @Operation(summary = "批量删除")
    @DeleteMapping
    public LMResult<Boolean> deleteBatch(@RequestBody List<Long> ids) {
        return LMResult.success(productClient.deleteBatch(ids));
    }

}


